package com.wc5818.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.wc5818.common.FileController;
import com.wc5818.config.ApplicationConfig;
import com.wc5818.entity.User;

@RestController
@RequestMapping("/tfy")
public class GetTFY {

	@Autowired
	ApplicationConfig config;
	
	@GetMapping(value = "/test")
	public ModelAndView test(HttpServletRequest req) {
		// UserEntity userEntity = getCurrentUser(req);
		User user = new User();
		user.setUserName("tom");
		user.setId(234);
		user.setPassWord("1");
		ModelAndView mv = new ModelAndView();
		mv.addObject("user", user);
		mv.setViewName("/tfy/user/show.html");
		return mv;
	}

	/***
	 * 文件上传
	 * 
	 * @param file
	 * @param model
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/fileUpload")
	public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile file, Model model,
			HttpServletRequest request) {
 
		
		  if (file.isEmpty()) {
	            System.out.println("文件为空空");
	        }
		
		FileController f = new FileController();
		String filename1 = f.fileUpload(file, model, request,config.getUploadpath());

		ModelAndView mv = new ModelAndView();
		mv.addObject("filename", config.getUploadmapping() + filename1);
		mv.setViewName("/tfy/user/show.html");
		return mv;

	}
	
	
	/***
	 * 文件上传 getFileUploadFileName
	 * 客户端文件上传后的
	 * @param file
	 * @param model
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/getFileUploadFileName")
	public String getFileUploadFileName(@RequestParam(value = "file") MultipartFile file, Model model,
			HttpServletRequest request) {
 
		
		  if (file.isEmpty()) {
	           return "0";
	        }
		
		FileController f = new FileController();
		String filename1 = f.fileUpload(file, model, request,config.getUploadpath());

		if(filename1.equals("0")) { return "0";}
		
		return config.getUploadmapping() + filename1;

	}
	

}
